﻿using System.Collections.Generic;
using System.Configuration;

namespace SeleniumToolkit.Config
{
    /// <summary>
    /// Configuration Element for a Collection of BrowserMap Elements
    /// </summary>
    public class BrowserMapElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        public BrowserMapElementCollection()
        {
            Initialize();
        }

        /// <summary>
        /// Initialize the BrowserMap with the default supported browsers
        /// </summary>
        protected virtual void Initialize()
        {
            Add("*iexplore");
            Add("*firefox");
            Add("*safari");
            Add("*opera");
            Add("*googlechrome");
            Add("*konqueror");
        }

        /// <summary>
        /// Creates a new BrowserMap Element
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new BrowserMapElement();
        }

        /// <summary>
        /// Gets the specified element
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BrowserMapElement)element).Key;
        }

        /// <summary>
        /// Indexer
        /// </summary>
        /// <param name="key">Element Key</param>
        /// <returns>BrowserMapElement</returns>
        public new BrowserMapElement this[string key]
        {
            get { return (BrowserMapElement)base.BaseGet(key); }
        }

        /// <summary>
        /// Adds a BrowserMap to the collection
        /// </summary>
        /// <param name="browserMap"></param>
        public void Add(BrowserMapElement browserMap)
        {
            base.BaseAdd(browserMap);
        }

        /// <summary>
        /// Adds a BrowserMap to the collection
        /// </summary>
        /// <param name="key"></param>
        public void Add(string key)
        {
            Add(new BrowserMapElement(key));
        }

        /// <summary>
        /// Converts the Collection into a Dictionary
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ToDictionary()
        {
            var dictionary = new Dictionary<string,string>();

            foreach (BrowserMapElement element in this)
            {
                dictionary.Add(element.Key,element.Value);
            }

            return dictionary;
        }
    }

    
}
